<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" :model="listQuery" class="filter-container">
      <el-form-item prop="id">
        <el-input v-model="listQuery.id" size="small" placeholder="ID" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="listQuery.name" size="small" placeholder="名称" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="veevacode">
        <el-input v-model="listQuery.veevacode" size="small" placeholder="VeevaCode" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="mrUserId">
        <el-input v-model="listQuery.mrUserId" size="small" placeholder="MrUserId" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="type">
        <el-select v-model="listQuery.type" size="small" placeholder="关联类型" filterable clearable @change="handleFilter">
          <el-option v-for="item in typeOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="property">
        <el-select v-model="listQuery.property" size="small" placeholder="属性" filterable clearable @change="handleFilter">
          <el-option v-for="item in propertyOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >筛选</el-button>
        <el-button
          size="small"
          @click="resetFilter"
        >重置</el-button>
        <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="toQrcodeNew"
        >新建二维码</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column type="expand">
        <template slot-scope="scope">
          <div class="table-expand-row">URL：{{ scope.row.url }}</div>
          <div class="table-expand-row">描述：{{ scope.row.notes }}</div>
          <div class="table-expand-row">封面：<img :src="scope.row.cover" alt="" width="100"></div>
          <div class="table-expand-row">封面描述：{{ scope.row.covernotes }}</div>
        </template>
      </el-table-column>
      <el-table-column label="ID" width="80">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="二维码" width="130" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.codeurl" alt="" width="100">
        </template>
      </el-table-column>
      <el-table-column label="名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="关联类型" width="130" align="center">
        <template slot-scope="scope">
          {{ scope.row.typename }}
        </template>
      </el-table-column>
      <el-table-column label="MrUserId" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.mrUserId }}
        </template>
      </el-table-column>
      <el-table-column label="属性" width="110" align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.property === 0"
            effect="plain"
            size="mini"
            type="info"
          >临时</el-tag>
          <el-tag
            v-if="scope.row.property === 1"
            effect="plain"
            size="mini"
            type="warning"
          >永久</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="Scene">
        <template slot-scope="scope">
          {{ scope.row.scene }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="URL类型" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.urltype }}
        </template>
      </el-table-column> -->
      <el-table-column label="扫码次数" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.num }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.settime | moment('YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="失效时间" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.expiretime | moment('YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="mini"
            @click="toQrcodeShow(scope.row.id)"
          >扫码详情</el-button>
          <el-button
            size="mini"
            @click="toQrcodeEdit(scope.row.id)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="fetchData" />
  </div>
</template>

<script>
import {
  getQrcodeType
} from '@/api/baseData'
import {
  getQrcodesList
} from '@/api/qrcodes'
import Pagination from '@/components/Pagination'
import {
  formatQuery
} from '@/utils'

export default {
  name: 'QrcodesList',
  components: {
    Pagination
  },
  data() {
    return {
      typeOptions: [],
      propertyOptions: [
        {
          name: '临时',
          id: 0
        },
        {
          name: '永久',
          id: 1
        }
      ],
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        id: formatQuery(this.$route.query.id, false),
        name: formatQuery(this.$route.query.name, false),
        veevacode: formatQuery(this.$route.query.veevacode, false),
        mrUserId: formatQuery(this.$route.query.mrUserId, false),
        type: formatQuery(this.$route.query.type, true),
        property: formatQuery(this.$route.query.property, true),
        page: 1,
        size: 20
      }
    }
  },
  // watch: {
  //   $route: {
  //     handler(route) {
  //       if (!this._.isNil(route.query.page) &&
  //         !this._.isEmpty(route.query.page)
  //       ) {
  //         this.listQuery.page = parseInt(route.query.page)
  //       } else {
  //         this.listQuery.page = 1
  //       }
  //     },
  //     immediate: true
  //   }
  // },
  created() {
    if (!this._.isNil(this.$route.query.page) &&
      !this._.isEmpty(this.$route.query.page)
    ) {
      this.listQuery.page = parseInt(this.$route.query.page)
    } else {
      this.listQuery.page = 1
    }
    this.fetchQrcodeType()
    this.fetchData()
  },
  methods: {
    fetchQrcodeType() {
      getQrcodeType().then(response => {
        // console.log(response)
        this.typeOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData() {
      this.listLoading = true
      const query = {
        ...this.$route.query,
        ...this.listQuery
      }
      this.$router.push({
        query
      }).catch(err => {
        console.log(err)
      })
      getQrcodesList(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.list = []
      this.fetchData()
    },
    resetFilter() {
      this.$refs.filterForm.resetFields()
      for (const i in this.listQuery) {
        if (i !== 'size') {
          this.listQuery[i] = undefined
        }
      }
      // console.log(this.listQuery)
      this.$router.push({
        query: {
          page: 1
        }
      }).catch(err => {
        console.log(err)
      })
      this.handleFilter()
    },
    toQrcodeNew() {
      this.$router.push('/qrcodes/new')
    },
    toQrcodeShow(id) {
      this.$router.push(`/qrcodes/list/${id}`)
    },
    toQrcodeEdit(id) {
      this.$router.push(`/qrcodes/edit/${id}`)
    }
  }
}
</script>
